﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using TechNewLogic.GraphIT;
using TechNewLogic.GraphIT.Hv;
using TechNewLogic.GraphIT.Hv.Legend;
using TechNewLogic.GraphIT.Hv.Vertical;

namespace GraphicsTestGeorgi
{
    /// <summary>
    /// Interaction logic for Graph.xaml
    /// </summary>
    public partial class Graph : Window
    {
        public Graph()
        {
            InitializeComponent();

            //Changes the bacakgraound of the graph field
            CurveDisplay.GeneralBackground = new SolidColorBrush(Color.FromRgb(0xcc,0xff,0x99));

            //Change the color of the text, cursors etc.
            CurveDisplay.GeneralForeground = new SolidColorBrush(Color.FromRgb(0,0,0));


            // Set the bounds of the time axis
            CurveDisplay.TimeDoublePlottingSystem.TimeAxis.SetBounds(
                DateTime.UtcNow.AddMinutes(-50),
                DateTime.UtcNow.AddMinutes(10));

            // Initialize the table legend and add it to the page
            var tableLegend = new TableLegend(
                CurveDisplay,
                "Term",
                "Location",
                "Aggregate",
                "Medium",
                "Measurement");

            tableLegendHolder.Content = tableLegend;

            // Initialize the tile legend and add it to the page
            var tileLegend = new TileLegend(
                CurveDisplay);
            tileLegend.Width = 250;
            tileLegend.TileWidth = 250;
            tileLegendHolder.Content = tileLegend;

            CreateCurve();
            CreateCurve1();

            AddCurveData();
            AddCurveData1();
        }

        //Curves Variables
        private TimeDoubleCurve _curve;
        private TimeDoubleCurve _curve1;

		private void CreateCurve()
		{
			// Create a new curve
			_curve = CurveDisplay.TimeDoublePlottingSystem.AddCurve(
				"kg", -20, 20, Colors.Red, RedrawTime.Ms500, AxisMatchingMode.UomAndBounds, 
				CurveDrawingMode.SimpleLine(),
				new FloatingCommaValueFormater(),
				new InterpolationValueFetchStrategy(),
				AxisFormat.Double,
				int.MaxValue);
			// We have to set some description for the curve so that it can be displayed in the table legend!
			_curve.Description.DescriptionText1 = "R2033";
			_curve.Description.DescriptionText2 = "House 20";
			_curve.Description.DescriptionText3 = "BHKW";
			_curve.Description.DescriptionText4 = "Cur. Val.";
			_curve.Description.DescriptionText5 = "Power Calc.";
		}

        private void CreateCurve1()
        {
            _curve1 = CurveDisplay.TimeDoublePlottingSystem.AddCurve("T", -10, 40, Colors.Black, RedrawTime.S10,
                      AxisMatchingMode.None, CurveDrawingMode.SimpleLine(),
                      new FloatingCommaValueFormater(),
                      new InterpolationValueFetchStrategy(),
                      AxisFormat.Double,
                      int.MaxValue);

            _curve1.Description.DescriptionText1 = "text1";
            _curve1.Description.DescriptionText2 = "asdf";
        }

        private void AddCurveData1()
        {
            var startTime = DateTime.UtcNow.AddMinutes(-40);
            var timeSpan = TimeSpan.FromHours(10);
            var data = EntryGenerator.GenerateRandomData(startTime, timeSpan, 5000, 40, -30, 0.5);
            _curve1.DataSeries.Append(data);
        }

		private void AddCurveData()
		{
			// 1.
			// Create some data and add it to the curve
			var startTime = DateTime.UtcNow.AddMinutes(-40);
			var timeSpan = TimeSpan.FromMinutes(20);
			var data = EntryGenerator.GenerateRandomData(
				startTime,
				timeSpan,
				5000,
				15,
				-15,
				0.2);
			_curve.DataSeries.Append(data);

			// 2.
			// Not, we append a gap
			var latestTime = startTime.Add(timeSpan);
			var undefinedValue = _curve.DataSeries.UndefinedValue;
			var gapEnd = latestTime.AddMinutes(10);
			_curve.DataSeries.Append(
				new[]
					{
						new TimeDoubleDataEntry(latestTime.AddMilliseconds(1), undefinedValue),
						new TimeDoubleDataEntry(gapEnd, _curve.DataSeries.UndefinedValue),
					});

			// 3.
			// Create some additional data after the gap
			var additionalData = EntryGenerator.GenerateRandomData(
				gapEnd.AddMilliseconds(1),
				TimeSpan.FromMinutes(10),
				5000,
				15,
				-15,
				0.2);
			_curve.DataSeries.Append(additionalData);
		}
	

    }
}
